import pygame

class Game:
    def __init__(self):
        self.player1 = "Player 1"
        self.player2 = "Player 2"
        self.current_player = self.player1
        self.done = False
        self.won = False
        self.pieces = []  # 棋子列表

        # 初始化 Pygame 库
        pygame.init()

        # 显示设置
        self.size = [600, 600]
        self.green = (0, 155, 0)
        self.black = (0, 0, 0)
        self.white = (255, 255, 255)
        self.screen = pygame.display.set_mode(self.size)

        # ...（省略其他初始化代码）

    def run_game(self):
        pygame.init()
        self.draw_board()  # 绘制初始棋盘

        while not self.done:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.done = True
                    break
                elif event.type == pygame.MOUSEBUTTONUP:
                    pos = pygame.mouse.get_pos()
                    self.handle_click(pos)

    def handle_click(self, pos):
        # 处理鼠标点击事件
        for piece in self.pieces:
            if piece.cx - piece.radius <= pos[0] <= piece.cx + piece.radius \
                    and piece.cy - piece.radius <= pos[1] <= piece.cy + piece.radius:
                self.handle_piece_click(piece)
                break

    def handle_piece_click(self, piece):
        # 处理棋子点击事件
        if piece.color == self.current_player:
            self.move_piece(piece)

    def move_piece(self, piece):
        # 处理棋子移动
        if self.last_move is not None:
            self.last_move.flip_color()

        dest = self.get_clicked_position()

        if piece.can_move_to(dest):
            piece.move(dest)
            self.last_move = piece
            self.switch_player()
            if self.check_for_winner():
                self.done = True

        self.draw_board()

    def get_clicked_position(self):
        # 根据鼠标点击的位置，获取对应的棋盘位置
        for pos, (cx, cy) in self.POSITION.items():
            if cx - piece.radius <= pos[0] <= cx + piece.radius \
                    and cy - piece.radius <= pos[1] <= cy + piece.radius:
                return pos
        return None

    def switch_player(self):
        # 切换玩家
        if self.current_player == self.player1:
            self.current_player = self.player2
        else:
            self.current_player = self.player1

    def check_for_winner(self):
        # 根据游戏规则判断是否有胜利者
        # 如果有胜利者，返回 True
        # 如果没有胜利者，返回 False
        if len(self.pieces) == 0:
            self.won = True
            return True
        return False

    def draw_board(self):
        # 填充背景色为绿色
        self.screen.fill(self.green)

        # 定义五角星顶点的坐标
        star_points = [
            (300, 0), (330, 150), (480, 150), (360, 240), (390, 390),
            (300, 300), (210, 390), (240, 240), (120, 150), (270, 150),
        ]

        # 绘制五角星，使用 pygame.draw.polygon 函数
        pygame.draw.polygon(self.screen, self.black, star_points, 3)

        # 画出棋子
        for piece in self.pieces:
            piece.draw()

        # 添加文本显示
        font = pygame.font.Font(None, 36)
        if self.done and not self.won:
            text = font.render(f"游戏结束，你输了，棋子剩余{len(self.pieces)}颗", True, self.white)
        elif self.done and self.won:
            text = font.render(f"游戏结束，你赢了，棋子剩余{len(self.pieces)}颗", True, self.white)
        else:
            text = font.render(f"当前玩家：{self.current_player}", True, self.white)
        text_rect = text.get_rect(center=(300, 550))
        self.screen.blit(text, text_rect)

        pygame.display.flip()

    def main(self):
        self.run_game()
        pygame.quit()  # 在游戏主循环结束后关闭 Pygame 库

if __name__ == "__main__":
    game = Game()
    game.main()